package xidian.vlab.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xidian.vlab.domin.*;
import xidian.vlab.domin.User.Teacher;
import xidian.vlab.dto.*;
import xidian.vlab.repository.RecordRepository;
import xidian.vlab.repository.User.TeacherRepository;
import xidian.vlab.service.LogService;
import xidian.vlab.service.RecordService;
import xidian.vlab.utils.ResUtil;

import java.util.*;

@Service("RecordService")
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordRepository recordRepo;
    @Autowired
    private TeacherRepository tchRepo;
    @Autowired
    private LogService logService;


    @Override
    public ResJsonDTO store(MsgDTO msg){
        System.out.println(msg);
        if (msg.getTid() == null) {
            return ResUtil.ERROR("教师ID错误!");
        }

        Teacher teacher = new Teacher();
        teacher.setTid(msg.getTid());
        Record record = new Record(
                msg.getTitle(),
                msg.getContent(),
                "待审核",
                new Date(),
                teacher
        );

        System.out.println("教师["+msg.getTid()+"]提交了一次申请。");
        recordRepo.save(record);
        return ResUtil.OK();
    }
    @Override
    public ResJsonDTO getList(QueryDTO query){
        ListDTO list = new ListDTO();
        List<MsgDTO> msgList = new ArrayList<>();
        if (query.getTid() != null){
            Teacher teacher = tchRepo.findById(query.getTid()).orElse(null);
            if (teacher == null) return ResUtil.ERROR("Can't find teacher "+query.getTid());
            for (Record record : teacher.getRecords()) {
                msgList.add(new MsgDTO(
                        record.getFid(),
                        teacher.getTid(),
                        teacher.getName(),
                        record.getTitle(),
                        record.getContent(),
                        record.getStatus(),
                        record.getCreateTime()
                ));
            }
            System.out.println("教师["+query.getTid()+"]获取申请记录列表成功");
        }
        else{
            for (Record record : recordRepo.findAll()) {
                msgList.add(new MsgDTO(
                        record.getFid(),
                        record.getTeacher().getTid(),
                        record.getTeacher().getName(),
                        record.getTitle(),
                        record.getContent(),
                        record.getStatus(),
                        record.getCreateTime()
                ));
            }
            System.out.println("管理员获取申请记录列表成功");
        }
        list.setMsgList(msgList);
        list.setTotal(msgList.size());//fix here
        return ResUtil.OK(list);
    }
    @Override
    public ResJsonDTO getNum() {
        return ResUtil.OK(recordRepo.countByStatus("待审核"));
    }
    @Override
    public ResJsonDTO updateRecord(QueryDTO query){
        List<Record> records = new ArrayList<>();
        if (query.getFid() != null) {//指定记录
            Record record = recordRepo.findById(query.getFid()).orElse(null);
            if (record == null) { return ResUtil.ERROR("Can't find record "+query.getFid()); }

            System.out.println("执行申请["+record.getFid()+"]中。。。");
            records.add(record);
        } else {//所有记录
            System.out.println("执行所有待审核申请中。。。");
            records = recordRepo.findAll();
        }
        for (Record record : records) {//待优化，只查找状态为待审核的记录
            if (record.getStatus().equals("待审核")) {
                record.setStatus(query.getStatus());
                recordRepo.save(record);
            }
        }
        System.out.println("全部申请处理完成");

        logService.addLog(new Log(
                query.getAid(),
                "管理员",
                "处理教室申请",
                "成功",
                null,
                new Date()
        ));
        return ResUtil.OK();
    }

}
